<?php
//文章控制器
class ArticleControl extends CommonControl{
	public function add(){
			if(IS_POST){
			$upload = new upload();
			$info = $upload->upload();
			if($info){
				$thumb = __ROOT__ . '/' . $info[0]['path'];//缩略图
			}
			$intro = mb_substr(strip_tags($_POST['con']), 0, 100, 'UTF-8') . '.....';//文章摘要

			$data = array(
				'title'	=> $this->_POST('title'),
				'thumb'	=> isset($thumb) ? $thumb : '',
				'cid'	=> $this->_POST('cid', 'intval'),
				'content'=> $_POST['con'],
				'click'	=> $this->_POST('click', 'intval'),
				'intro'	=> $intro,
				'time'	=> time(),
				); 
			M('article')->add($data);
			$this->success('发表成功！');
		}
		$category = M('category')->field('cid,cname')->select();
		$this->assign('category', $category);
		$this->display();
	}

	//文章列表
	public function article(){
		$field = 'aid,title,cname,click,time,article.cid,istop';
		$where = array('isdel'=>0);

		if($cid = $this->_GET('cid','intval')){
			$where['cid'] = $cid;
		}

		//分页
		$total = M('article')->where($where)->count();
		$page = new page($total, 10, 4, 2);
		$this->assign('page',$page->show());

		$article = K('Article')->field($field)->where($where)->order('istop DESC,aid DESC')->select($page->limit());
		$this->assign('article',$article);


		$this->display();
	}

	//置顶
	public function top(){
		$w = $this->_GET('w','intval');
		$aid = $this->_GET('aid','intval');
		if ($w==1) {
			# code...
			M('article')->where(array('aid'=>$aid))->save(array('istop'=>1));
			$this->success('置顶成功！！！');
		}else{
			M('article')->where(array('aid'=>$aid))->save(array('istop'=>0));
			$this->success('取消成功！！！');
		}
	}

	//编辑
	public function edit(){
		if(IS_POST){
			$upload = new upload();
			$info = $upload->upload();

			$intro = mb_substr(strip_tags($_POST['con']), 0, 100, 'UTF-8') . '.....';

			$data = array(
				'title'	=> $this->_POST('title'),
				'cid'	=> $this->_POST('cid', 'intval'),
				'content'=> $_POST['con'],
				'click'	=> $this->_POST('click', 'intval'),
				'intro'	=> $intro,
				'time'	=> time(),
				);
			if($info){
				$thumb = __ROOT__ . '/' . $info[0]['path'];
				$data['thumb'] = $thumb;
			}
			$aid = $this->_POST("aid", 'intval');
			M('article')->where(array('aid'=>$aid))->save($data);

			$this->success('修改成功！');
		}
		

		$category = M('category')->field('cid,cname')->select();
		$this->assign('category',$category);

		$aid = $this->_GET('aid','intval');
		$article = M('article')->where(array('aid'=>$aid))->field('title,thumb,cid,content,click')->find();
		$this->assign('article',$article);
		$this->display();
	}

	//删除到回收站
	public function del(){
		$aid = $this->_GET('aid','intval');
		M('article')->where(array('aid'=>$aid))->save(array('isdel'=>1));
		$this->success('删除成功！！！');

	}

	//回收站显示
	public function recycle(){
		$field = 'aid,title,cname,click,time,article.cid';
		$where = array('isdel'=>1);

		if($cid=$this->_GET('cid', 'intval')){
			$where['cid'] = $cid;
		}
		$total = M('article')->where($where)->count();
		$page = new page($total, 1, 4, 2);
		$this->assign('page', $page->show());

		$article = K('article')->field($field)->where($where)->order('aid ASC')->select($page->limit());
		$this->assign('article', $article);

		$this->display();
	}

	//恢复
	public function recover(){
		$aid = $this->_GET('aid','intval');
		M('article')->where(array('aid'=>$aid))->save(array('isdel'=>0));
		$this->success('恢复成功！！！');
	}

	//彻底删除
	public function true_del(){
		$aid = $this->_GET('aid','intval');
		M('article')->where(array('aid'=>$aid))->delete();
		M('comment')->where(array('aid'=>$aid))->delete();
		$this->success('删除成功！！！');
	}








}
?>